package com.otitan.forest.right.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otitan.forest.right.dto.SysOrganizationDto;
import com.otitan.forest.right.dto.TreeDto;
import com.otitan.forest.right.entity.SysOrganizationEntity;
import com.otitan.forest.right.service.SysOrganizationService;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 组织机构
 *
 * @author xiazw
 * @email *****@mail.com
 * @date 2020-08-04 14:19:44
 */
@Api(tags = "组织机构API")
@RestController
@RequestMapping("/sysOrganization")
@RequiresRoles("admin")
public class SysOrganizationController {

    @Autowired
    private SysOrganizationService sysOrganizationService;

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "返回机构树")
    @GetMapping("/getSysOrganizationTree")
    public DataResult getSysOrganizationTree() {
        return DataResult.success(sysOrganizationService.getSysOrganizationTree());
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "根据组织机构父级ID获取子组织机构信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", required = true, dataType = "string", value = "父级组织机构ID"),
            @ApiImplicitParam(name = "type", dataType = "boolean", value = "类型，true：所有下级组织机构，false：一级组织机构数据"),
            @ApiImplicitParam(name = "level",dataType = "int",  value = "parentId对应的行政区划等级")
    })
    @GetMapping("/getSysOrganizationByParentId")
    public DataResult getSysOrganizationById(String parentId, Boolean type, Integer level) {
        return DataResult.success(sysOrganizationService.getSysOrganizationById(parentId, type, level));
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "获取市、县行政区数据")
    @GetMapping("/getCityAndCounty")
    public DataResult getCityAndCounty() {
        String levels = "2,3";
        List<TreeDto> cityAndCounty = sysOrganizationService.getOrgByLevels(levels);
        return DataResult.success(cityAndCounty);
    }

    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public DataResult add(@RequestBody SysOrganizationEntity sysOrganization) {
        sysOrganizationService.checkInsertSysOrganization(sysOrganization);
        sysOrganizationService.save(sysOrganization);
        return DataResult.success();
    }

    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysOrganizationService.checkDeleteSysOrganization(ids);
        sysOrganizationService.removeByIds(ids);

        return DataResult.success();
    }

    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public DataResult update(@RequestBody SysOrganizationEntity sysOrganization) {
        sysOrganizationService.checkUpdateSysOrganization(sysOrganization);
        sysOrganizationService.updateById(sysOrganization);
        return DataResult.success();
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "查询分页数据")
    @PostMapping("/listByPage")
    public DataResult findListByPage(@RequestBody SysOrganizationEntity sysOrganization) {
        Page page = new Page(sysOrganization.getPage(), sysOrganization.getSize());
        LambdaQueryWrapper<SysOrganizationEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(SysOrganizationEntity::getId, sysOrganization.getId());
        IPage<SysOrganizationEntity> iPage = sysOrganizationService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "根据Id查询实体")
    @GetMapping("/findOne/{id}")
    public DataResult findOneById(@PathVariable String id) {
        SysOrganizationEntity entityObj = sysOrganizationService.getById(id);
        return DataResult.success(entityObj);
    }


    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @ApiOperation(value = "批量新增/修改")
    @PostMapping("/addOrUpdateSysOrganization")
    public DataResult addOrUpdateSysOrganization(@RequestBody SysOrganizationDto sysOrganizationDto) {
        sysOrganizationService.addOrUpdateSysOrganization(sysOrganizationDto);
        return DataResult.success();
    }

}
